package com.letian.web.controller.im;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.letian.common.core.domain.entity.SysUser;
import com.letian.common.core.domain.model.UsersVO;
import com.letian.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.letian.common.annotation.Log;
import com.letian.common.core.controller.BaseController;
import com.letian.common.core.domain.AjaxResult;
import com.letian.common.enums.BusinessType;
import com.letian.im.domain.ImGroupUser;
import com.letian.im.service.IImGroupUserService;
import com.letian.common.utils.poi.ExcelUtil;
import com.letian.common.core.page.TableDataInfo;

/**
 * 群组用户Controller
 * 
 * @author bmz
 * @date 2024-01-28
 */
@RestController
@RequestMapping("/im/group/user")
public class ImGroupUserController extends BaseController
{
    @Autowired
    private IImGroupUserService imGroupUserService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询群组用户列表
     */
    @PreAuthorize("@ss.hasPermi('im:user:list')")
    @GetMapping("/relation/list")
    public TableDataInfo list(ImGroupUser imGroupUser)
    {
        startPage();
        List<ImGroupUser> list = imGroupUserService.selectImGroupUserList(imGroupUser);
        return getDataTable(list);
    }

    /**
     * 查询群组用户列表
     */
    @PreAuthorize("@ss.hasPermi('im:user:list')")
    @GetMapping("/list")
    public AjaxResult userList(ImGroupUser imGroupUser)
    {
        List<ImGroupUser> list = imGroupUserService.selectImGroupUserList(imGroupUser);
        Long[] ids = (Long[])list.stream().map(ImGroupUser::getUserId).toArray(Long[]::new);
        List<SysUser> userList = sysUserService.selectUserByIds(ids);
        List<UsersVO> userVOS = new ArrayList<>();
        for (SysUser user : userList) {
            UsersVO userVO = new UsersVO();
            BeanUtils.copyProperties(user, userVO);
            userVOS.add(userVO);
        }
        return success(userVOS);
    }

    /**
     * 导出群组用户列表
     */
    @PreAuthorize("@ss.hasPermi('im:user:export')")
    @Log(title = "群组用户", businessType = BusinessType.EXPORT)
    @PostMapping("/relation/export")
    public void export(HttpServletResponse response, ImGroupUser imGroupUser)
    {
        List<ImGroupUser> list = imGroupUserService.selectImGroupUserList(imGroupUser);
        ExcelUtil<ImGroupUser> util = new ExcelUtil<ImGroupUser>(ImGroupUser.class);
        util.exportExcel(response, list, "群组用户数据");
    }

    /**
     * 获取群组用户详细信息
     */
    @PreAuthorize("@ss.hasPermi('im:user:query')")
    @GetMapping(value = "/relation/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(imGroupUserService.selectImGroupUserById(id));
    }

    /**
     * 新增群组用户
     */
    @PreAuthorize("@ss.hasPermi('im:user:add')")
    @Log(title = "群组用户", businessType = BusinessType.INSERT)
    @PostMapping("/relation")
    public AjaxResult add(@RequestBody ImGroupUser imGroupUser)
    {
        return toAjax(imGroupUserService.insertImGroupUser(imGroupUser));
    }

    /**
     * 修改群组用户
     */
    @PreAuthorize("@ss.hasPermi('im:user:edit')")
    @Log(title = "群组用户", businessType = BusinessType.UPDATE)
    @PutMapping("/relation")
    public AjaxResult edit(@RequestBody ImGroupUser imGroupUser)
    {
        return toAjax(imGroupUserService.updateImGroupUser(imGroupUser));
    }

    /**
     * 删除群组用户
     */
    @PreAuthorize("@ss.hasPermi('im:user:remove')")
    @Log(title = "群组用户", businessType = BusinessType.DELETE)
	@DeleteMapping("/relation/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(imGroupUserService.deleteImGroupUserByIds(ids));
    }
}
